﻿using HapperComm;
using ICarlifeRespository;
using ICarlifeService;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;

namespace CarlifeService
{
    public class BaseServer<T> : IBaseServer<T> where T : class, new()
    {
        private readonly IBasePespository<T> db;
        private readonly IUOfWork uOfWork;
        public BaseServer(IBasePespository<T> db, IUOfWork uOfWork)
        {
            this.db = db;
            this.uOfWork = uOfWork;
        }
        #region EF

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Add(T entity)
        {
            db.Add(entity);
            return uOfWork.SaveChanges();
        }
        /// <summary>
        /// 范围添加
        /// </summary>
        /// <param name="enitity"></param>
        /// <returns></returns>
        public int AddRange(List<T> enitity)
        {
            db.AddRange(enitity);
            return uOfWork.SaveChanges();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Delete(T entity)
        {
            db.Delete(entity);
            return uOfWork.SaveChanges();
        }

       
        /// <summary>
        /// 显示
        /// </summary>
        /// <returns></returns>
        public List<T> GetAll()
        {
            return db.GetAll();
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public T GetById(int id)
        {
            return db.GetById(id);
        }
       /// <summary>
       /// 修改
       /// </summary>
       /// <param name="entity"></param>
       /// <returns></returns>

   
        public int Update(T entity)
        {
            db.Update(entity);
            return uOfWork.SaveChanges();
        }

        #endregion
        #region Dapper泛型
        public async Task<int> Delete(List<T> t)
        {
            var tableName = ReflecationHelper.GetEntityTableName<T>();
            string sql = $"delete from {tableName}";

            var result = await db.Execute(sql, t);
            return result;
        }
        public async Task<int> Insert(T t)
        {
            string sql = ReflecationHelper.GetDapperInsertSqlString(t);
            var result = await db.Execute(sql, t);
            return result;
        }

        public async Task<int> Insert(List<T> t)
        {
            string sql = ReflecationHelper.GetDapperInsertSqlString(t[0]);
            var result = await db.Execute(sql, t);
            return result;
        }

        public async Task<List<T>> Select()
        {
            string sql = $"select * from {ReflecationHelper.GetEntityTableName<T>()}";
            var result = await db.Query(sql);
            return result;
        }

        public async Task<List<T>> Select(object param)
        {
            string sql = $"select * from {ReflecationHelper.GetEntityTableName<T>()}";
            var result = await db.Query(sql, param);
            return result;
        }


        public async Task<int> Updates(T t)
        {
            string sql = ReflecationHelper.GetDapperUpdateSqlString(t);
            var result = await db.Execute(sql, t);
            return result;
        }
        #endregion
        #region Sql语句执行
#pragma warning disable CS8625 // 无法将 null 字面量转换为非 null 的引用类型。
        public async Task<int> Insert(string sql, object param, IDbTransaction tran = null)
#pragma warning restore CS8625 // 无法将 null 字面量转换为非 null 的引用类型。
        {
            int result = await db.Execute(sql, param);
            return result;
        }
#pragma warning disable CS8625 // 无法将 null 字面量转换为非 null 的引用类型。
        public async Task<int> Update(string sql, object param, IDbTransaction tran = null)
#pragma warning restore CS8625 // 无法将 null 字面量转换为非 null 的引用类型。
        {
            int result = await db.Execute(sql, param);
            return result;
        }
#pragma warning disable CS8625 // 无法将 null 字面量转换为非 null 的引用类型。
        public async Task<int> Delete(string sql, object param, IDbTransaction tran = null)
#pragma warning restore CS8625 // 无法将 null 字面量转换为非 null 的引用类型。
        {
            int result = await db.Execute(sql, param);
            return result;
        }

        public async Task<List<T>> Select(string sql, object param)
        {
            var result = await db.Query(sql, param);
            return result;
        }
        public async Task<List<T>> Select(string sql, object param, CommandType commandType = CommandType.StoredProcedure)
        {
            var result = await db.Query(sql, param, commandType);
            return result;
        }
        #endregion
    }
}
